// pages/yao/yao.js
Page({

      /**
       * 页面的初始数据
       */
      data: {
            day:1,
            name:'',
            phone:'',
            id:'',
            focus1:false,
            focus2:false,
            focus3:false,
            disabled:false,
            submitText:'添加许可'
      },
      handleList(){
            wx.navigateTo({
                  url: '/pages/yaoList/yaoList',
            })
      },
      /**编辑邀请卡 */
      editYao(){
            this.setData({
                  submitText:'保存许可',
                  disabled:false
            })
      },

      // 天数输入框
      dayInput(e){
            if(!isNaN(e.detail.value)){
                  this.setData({
                        day:e.detail.value
                  })
            }else{
                  wx.showToast({
                        title: '需要输入数字',
                        icon:'error'
                  })
                  this.setData({
                        day:1
                  })
            }
      },

      // 姓名输入框
      nameInput(e){
            this.setData({
                  name:e.detail.value
            })
      },

      // 电话输入框
      phoneInput(e){
            this.setData({
                  phone:e.detail.value
            })
      },
      

      // 添加许可
      addInvita(){
            let userInfo=wx.getStorageSync('userInfo');
            if(userInfo.addr != undefined && userInfo.addr != "null" && userInfo.addr != "" && userInfo.addr != null){
                  if(this.data.day==""  || this.data.day==null){
                        wx.showToast({
                              title: '天数不能为空',
                              icon:'error'
                        })
                        this.setData({
                              focus1:true
                        })
                  }else if(this.data.name==""  || this.data.name==null){
                        wx.showToast({
                              title: '姓名不能为空',
                              icon:'error'
                        })
                        this.setData({
                              focus2:true
                        })
                  }else if(this.data.phone==""  || this.data.phone==null){
                        wx.showToast({
                              title: '电话不能为空',
                              icon:'error'
                        })
                        this.setData({
                              focus3:true
                        })
                  }
                  // 提交邀请卡
                  this.invite();
                  
            }else{
                  this.isInfo();
            }
      },
      /**检测手机号是否合法 */
      testPhone(phone){
            var myreg=/^[1][3,4,5,7,8,9][0-9]{9}$/;
            if (!myreg.test(phone)) {
                  return false;
            } else {
                  return true;
            }
      },
      // 提交邀请卡
      invite(){
            if(!(this.testPhone(this.data.phone))){
                  wx.showToast({
                        title: '手机号格式有误',
                        icon:'error'
                  })
                  return;
            }
            
            wx.showLoading({
              title: '添加许可',
            })
            let invitee={
                  name:this.data.name,
                  phone:this.data.phone
            }
            invitee=JSON.stringify(invitee);
            let inviter=wx.getStorageSync('userId');
            let addr=wx.getStorageSync('userInfo').addr;
            let inviteInfo={
                  invitee:invitee,
                  inviter:inviter,
                  addr:addr,
                  day:Math.floor(this.data.day),
                  id:this.data.id
            }

            wx.request({
                  url: getApp().globalData.host+'/client/invite/addInvite',
                  method:'POST',
                  data:inviteInfo,
                  header: {
                        'content-type': 'application/x-www-form-urlencoded'
                  },
                  success:res=>{
                        this.setData({
                              id:res.data.data.inviteId,
                              disabled:true
                        })
                        wx.showToast({
                          title: '添加成功'
                        })
                  },
                  complete:()=>{
                        wx.hideLoading();
                  }
            })

      },

      // 完善信息
      isInfo(){
            wx.showModal({
                  title: '未完善信息',
                  content: '需要补全房屋信息',
                  success: (res)=> {
                        if (res.confirm) {
                              wx.navigateTo({
                                    url:'/pages/userInfo/userInfo'
                              })

                        } else if (res.cancel) {
                              // 点击取消后返回主页面
                              wx.switchTab({
                                    url: '/pages/index/index'
                              })
                              wx.showToast({
                                    title: '需要完善信息',
                                    icon: 'error'
                              })
                        }
                  }
            })
      },



      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
            if(options.loseDate !=undefined && options.loseDate!=""){
                  let day;
                  if((options.loseDate)<(new Date().getTime())){
                        day=0;
                  }else{
                        day=Math.ceil(((options.loseDate)-(new Date().getTime()))/86400000);
                  }
                  
                  
                  this.setData({
                        name:options.name,
                        id:options.id,
                        phone:options.phone,
                        day:day
                  })
            }
      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
            let userInfo=wx.getStorageSync("userInfo");
            if(userInfo.nickName==null || userInfo.nickName==""){
                  wx.showModal({
                        title: '未登录',
                        content: '是否进行登录？',
                        success: (res)=> {
                              if (res.confirm) {
                                    wx.navigateTo({
                                          url:'/pages/login/login'
                                    })

                              } else if (res.cancel) {
                                    // 点击取消后返回主页面
                                    wx.switchTab({
                                          url: '/pages/index/index'
                                    })
                                    wx.showToast({
                                          title: '需要登录',
                                          icon: 'error'
                                    })
                              }
                        }
                  })
            }
            if(this.data.id!="" && this.data.id!=undefined){
                  this.setData({
                        disabled:true
                  })
            }
      },

      /**
       * 用户点击右上角分享
       */
      onShareAppMessage: function () {
            const id=this.data.id;
            return {
                  title: '邀请卡',
                  path: "pages/QRCode/QRCode?id="+id+"&isShare="+true,
                  imageUrl:'https://z3.ax1x.com/2021/09/19/43LVCF.png'
            }
      }
})